package com.example.actionbar;

import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		ActionBar bar = getActionBar();
		//Seta o icone da app como clicável
		bar.setDisplayHomeAsUpEnabled(true);
		//bar.hide();
		//bar.show();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		//getMenuInflater().inflate(R.menu.main, menu);
		
		CreateMenu(menu);
		return true;
	}

	private void CreateMenu(Menu menu) {
		
		MenuItem item1 = menu.add(0,0,0,"Item 1");
		item1.setIcon(R.drawable.ic_launcher);
		item1.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
		
		MenuItem item2 = menu.add(0,1,1,"Item 2");
		item2.setIcon(R.drawable.ic_launcher);
		item2.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
		
		MenuItem item3 = menu.add(0,2,2,"Item 3");
		item3.setIcon(R.drawable.ic_launcher);
		item3.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
		
		MenuItem item4 = menu.add(0,3,3,"Item 4");
		item4.setIcon(R.drawable.ic_launcher);
		item4.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
		
		MenuItem item5 = menu.add(0,4,4,"Item 5");
		item5.setIcon(R.drawable.ic_launcher);
		item5.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
		
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		return MenuChoice(item);
	}

	private boolean MenuChoice(MenuItem item) {
		
		switch (item.getItemId()) { 
		
		//Identifica se o icone da app foi clicado
		case android.R.id.home:
			Toast.makeText(this, "Clicou no Icone App", Toast.LENGTH_SHORT).show();
			
			//A intent abaixo limpa todas as activities no back stack, garantindo
			// que ao clicar no icone do app, vai voltar para o home da aplicação
			Intent i = new Intent(this, MainActivity.class);
			i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(i);
			
			return true;
		case 0:
				Toast.makeText(this, "Clicou no item 0", Toast.LENGTH_SHORT).show();
				return true;
		case 1:
			Toast.makeText(this, "Clicou no item 1", Toast.LENGTH_SHORT).show();
			return true;
		case 2:
			Toast.makeText(this, "Clicou no item 2", Toast.LENGTH_SHORT).show();
			return true;	
		case 3:
			Toast.makeText(this, "Clicou no item 3", Toast.LENGTH_SHORT).show();
			return true;	
		case 4:
			Toast.makeText(this, "Clicou no item 4", Toast.LENGTH_SHORT).show();
			return true;	
		
		}
		
		return false;
	}

}
